package com.c.ctgproject.ui.service

import androidx.recyclerview.widget.GridLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.MenuAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.MenuBean
import com.c.ctgproject.entity.service.ServiceOrderCountEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserver
import com.c.ctgproject.http.ObserverListener
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.exitApp
import com.c.ctgproject.utils.getGson
import com.c.ctgproject.utils.showPopupWindow
import kotlinx.android.synthetic.main.activity_customer_service.*
import okhttp3.ResponseBody

/**
 *作者:LiBW
 *创建日期: 2021/6/29
 *描述:客服 页面
 */
class CustomerServiceHomeActivity : BaseActivity() {

    private val mList = ArrayList<MenuBean>()
    private val mAdapter by lazy { MenuAdapter(this, mList) }
    override fun event() {
        mList.add(MenuBean("总订单管理", R.drawable.icon_service_all,0))
        mList.add(MenuBean("预订单管理", R.drawable.icon_service_yu,0))
        mList.add(MenuBean("意向订单管理", R.drawable.icon_service_yi,0))
        mList.add(MenuBean("已取消订单", R.drawable.icon_service_qu,0))
        mList.add(MenuBean("已完成订单", R.drawable.icon_service_wan,0))
        mList.add(MenuBean("回退订单", R.drawable.icon_service_tui,0))
        mList.add(MenuBean("待自送订单", R.drawable.icon_service_dai,0))
        mList.add(MenuBean("物流管理", R.drawable.icon_service_wuliu,0))
        mMenuRV.adapter = mAdapter
        mMenuRV.layoutManager=GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false)
    }
    private val listener=object:ObserverListener{
        override fun onNext(t: ResponseBody) {
            val str=t.string().toString()
            MyLog.d("订单数统计回调:$str")
            val data= getGson().fromJson(str,ServiceOrderCountEntity::class.java).data
            //预订单数量
           data?.pending?.let {
               mList[1].count=it
           }
            //待自送
//           data?.selfDelivery?.let {
//               mList[6].count=it
//           }
            //意向订单
            data?.talk?.let {
                mList[2].count=it
            }
            //回退订单
            data?.returned?.let {
                mList[5].count=it
            }
            mAdapter.notifyDataSetChanged()

        }

        override fun onError(error: String) {

        }
    }

    override fun onResume() {
        super.onResume()
        ApiMethods.serviceOrderCount(MyObserver(this,listener,false))

    }



    override fun click() {
        mSettingIV.setOnClickListener {
            showPopupWindow(it)
        }
    }

    override fun onBackPressed() {
        exitApp()
    }

    override fun getLayout() = R.layout.activity_customer_service

}